define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'choi/course_refund/index' + location.search,
                    add_url: 'choi/course_refund/add',
                    edit_url: 'choi/course_refund/edit',
                    del_url: 'choi/course_refund/del',
                    multi_url: 'choi/course_refund/multi',
                    table: 'course_refund',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'course_refund_id',
                sortName: 'course_refund_id',
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'course_refund_id', title: __('Course_refund_id')},
                        {field: 'course_order_id', title: __('Course_order_id')},
                        {field: 'course_order.order_no', title: __('订单号'), operate: false},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'user.nickname', title: __('昵称'), operate: false},
                        {field: 'order_no', title: __('Order_no')},
                        // {field: 'transaction_id', title: __('Transaction_id')},
                        {field: 'total_fee', title: __('Total_fee'), operate:'BETWEEN'},
                        {field: 'num', title: __('Num')},
                        {field: 'reason', title: __('Reason'), operate: 'LIKE'},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (e, data) {
                            if (data.status == 1) {
                                return ` <a href="javascript:;" class="btn btn-xs btn-warning btn-refuse" data-id="`+data.course_refund_id+`">拒绝</a> ` +
                                    ` <a href="javascript:;" class="btn btn-xs btn-success btn-agree" data-id="`+data.course_refund_id+`">同意</a>`;
                            } else {
                                return ``;
                            }
                        }}
                    ]
                ]
            });

            //拒绝退款
            table.on('click', '.btn-refuse', function () {
                var id = $(this).data('id')
                layer.open({
                    title: '退款操作',
                    content: '您确定要拒绝吗？',
                    yes: function () {
                        $.post('/keyu.php/choi/course_refund/refuse', {id: id}, function (res) {
                            if (res.code) {
                                layer.msg(res.msg)
                                setTimeout(function () {
                                    window.location.reload()
                                }, 1000)
                            } else {
                                layer.msg(res.msg)
                            }
                        })
                    }
                });
            })

            //同意退款
            table.on('click', '.btn-agree', function () {
                var id = $(this).data('id')
                layer.open({
                    title: '同意退款',
                    content: '您确定同意退款吗？',
                    yes: function () {
                        $.post('/keyu.php/choi/course_refund/agree', {id: id}, function (res) {
                            if (res.code) {
                                layer.msg(res.msg)
                                setTimeout(function () {
                                    window.location.reload()
                                }, 1000)
                            } else {
                                layer.msg(res.msg)
                            }
                        })
                    }
                });
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'choi/course_refund/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'choi/course_refund/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'choi/course_refund/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        refuse: function () {
            Controller.api.bindevent();
        },
        agree: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});